package com.example.mapp.buildSql;

import org.apache.ibatis.jdbc.SQL;

import com.example.ext.entity.ExtApplyRepair;

/**
 * Created by douyx on 2018/03/25.
 */
public class ApplyRepairMappSql {

	public String queryList(ExtApplyRepair extApplyRepair) {
		String select = " a.*, b.party_name as maintenancePartyName,d.party_name as usedPartyName, c.elevator_num as elevatorNum ";
		String table = " apply_repair a left join party b on a.maintenance_party_id = b.id left join elevator c on a.elevator_id=c.id "
				+ " left join party d on a.used_party_id = d.id ";
		String whereParam = "";
		if (null != extApplyRepair.getUserId() && !"".equals(extApplyRepair.getUserId())) {
			whereParam += " and (c.safe_user_id = #{userId} or c.maintenance_user_first = #{userId} or c.maintenance_user_second = #{userId} )";
		}
		if (null != extApplyRepair.getApplyTimeFrom() && !"".equals(extApplyRepair.getApplyTimeFrom())) {
			whereParam += " and a.apply_time >= #{applyTimeFrom}";
		}
		if (null != extApplyRepair.getApplyTimeTo() && !"".equals(extApplyRepair.getApplyTimeTo())) {
			whereParam += " and a.apply_time <= #{applyTimeTo}";
		}
		if (null != extApplyRepair.getApproveStatus() && !"".equals(extApplyRepair.getApproveStatus())) {
			whereParam += " and a.approve_status = #{approveStatus}";
		}
		if (null != extApplyRepair.getDealStatus() && !"".equals(extApplyRepair.getDealStatus())) {
			whereParam += " and a.deal_status = #{dealStatus}";
		}
		if (null != extApplyRepair.getApplyType() && !"".equals(extApplyRepair.getApplyType())) {
			whereParam += " and a.apply_type = #{applyType}";
		}
		if (null != extApplyRepair.getUsedPartyId() && !"".equals(extApplyRepair.getUsedPartyId())) {
			whereParam += " and a.used_party_id = #{usedPartyId}";
		}
		if (null != extApplyRepair.getMaintenancePartyId() && !"".equals(extApplyRepair.getMaintenancePartyId())) {
			whereParam += " and a.maintenance_party_id = #{maintenancePartyId}";
		}
		if (null != extApplyRepair.getElevatorId() && !"".equals(extApplyRepair.getElevatorId())) {
			whereParam += " and a.elevator_id = #{elevatorId}";
		}
		String where = " 1=1 " + whereParam;
		return new SQL() {
			{
				SELECT(select);
				FROM(table);
				WHERE(where);
				ORDER_BY(" a.apply_time desc ");
			}
		}.toString();
	}
}
